package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.SInsuranceFeeService;
import cn.huiyunche.base.service.mappers.SInsuranceFeeMapper;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.SInsuranceFee;
import cn.huiyunche.base.service.model.SInsuranceFeeExample;
import cn.huiyunche.base.service.vo.SOrderFeeDetailVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.builder.BuilderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;

@Service
public class SInsuranceFeeServiceImpl implements SInsuranceFeeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SInsuranceFeeServiceImpl.class);

    @Autowired
    private BFeeModelService bFeeModelService;

    @Autowired
    private BFeeTypeService bFeeTypeService;

    @Autowired
    private SInsuranceFeeMapper sInsuranceFeeMapper;

    public SOrderFeeDetailVo queryInsuranceFee(BigDecimal estValue, int amount, Boolean hasInsurance) {
        if (estValue == null) {
            LOGGER.error("queryInsurance estValue must not be null!");
            throw new BuilderException("估值不能为空!");
        }

        // 总提车费
        BigDecimal total = BigDecimal.ZERO.setScale(2);

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.INTERCITY_ORDER).getId(), Arrays.asList(BFeeTypeEnum.INSURANCE.getValue())).get(0);

        SInsuranceFee insurance = null;
        if (hasInsurance) {
            if (0 == amount) {
                insurance = bFeeTypeService.getSInsuranceFeesByFeeCode(feeType, estValue);
                total = insurance.getPrice().multiply(feeType.getDiscount()).setScale(2, RoundingMode.UP);
            } else {
                insurance = bFeeTypeService.getSInsuranceFeesByFeeCode(feeType, estValue.divide(BigDecimal.valueOf(amount), 2, RoundingMode.UP).setScale(0, RoundingMode.UP));
                total = insurance.getPrice().multiply(BigDecimal.valueOf(amount)).multiply(feeType.getDiscount()).setScale(2, RoundingMode.UP);
            }
        }

        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());
        vo.setCost(total.setScale(0, RoundingMode.UP).setScale(2));

        return vo;
    }

    @Override
    public SOrderFeeDetailVo queryIncracityInsuranceFee() {

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.CITYWIDE_ORDER).getId(), Arrays.asList(BFeeTypeEnum.INSURANCE.getValue())).get(0);
        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());

        List<SInsuranceFee> list = this.getList(feeType.getId());
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("queryIncracityInsuranceFee list is empty");
            throw new IllegalArgumentException("查询保险费失败");
        }

        vo.setCost(list.get(0).getPrice());
        vo.setActualAmount(vo.getCost());

        return vo;
    }

    @Override
    public List<SInsuranceFee> getList(Long feeTypeId) {
        LOGGER.info("getList param : {}", feeTypeId);
        if (null == feeTypeId) {
            LOGGER.error("getList param feeTypeId must not bull");
            throw new IllegalArgumentException("费用类型主键不能为空");
        }

        SInsuranceFeeExample example = new SInsuranceFeeExample();
        example.createCriteria().andFeeTypeIdEqualTo(feeTypeId);

        return sInsuranceFeeMapper.selectByExample(example);
    }

}
